package utils;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/*
 * http://sites.google.com/site/uhilger/artikel_index/java_http_xml
 */
public class HTTPRequest {
    public HTTPRequest() {
        super();
    }
    public static InputStream get(String urlStr) throws Exception {
        return readFrom(urlStr, null);
    }
    public static InputStream post(String urlStr, String post)
        throws Exception
    {
        return readFrom(urlStr, post);
    }
    private static InputStream readFrom(String urlStr, String postStr)
        throws MalformedURLException, IOException
    {
        URLConnection conn = new URL(urlStr).openConnection();
        conn.setDoInput(true);
        if (postStr != null && postStr.length() > 0) {
            conn.setDoOutput(true);
            DataOutputStream output =
                    new DataOutputStream(conn.getOutputStream());
            output.writeBytes(postStr);
            output.flush();
            output.close();
        }
        return conn.getInputStream();
    }
}
